/*
Node aka, phone/person/detected
 Jonathan Leang 2010
 Capstone@jonathanleang.com
 */

public class Node {
  // user PMP API data
  int u_id;
  String u_date;
  String u_hostname;
  String u_type;
  String u_mac;
  float flyRate = 5;
  String m_mode;
  PointArray path;
  Point oldPoint;
  
  // Location of node on the screen, most of the time it is near the siffer 
  // when it is moving it will leave the sniffer and fly there
  float locationtX = 0;
  float locationtY = 0;
  float locationtZ = 0;
  float locationX = 0;
  float locationY = 0;
  float locationZ = 0;
  int newloc = 1;
  float locationR;
  AABB node;
  
  Node(int u_id, int u_date, String u_hostname, String u_type, String u_mac) {
  }
  Node(int u_id, String u_date, String u_hostname, String u_type, String u_mac) {
    this.u_id = u_id;
    this.u_date = u_date;
    this.u_hostname = u_hostname;
    this.u_type = u_type;
    this.u_mac = u_mac;
    node=new AABB(new Vec3D(0,0,0),new Vec3D(10,10,10));
    this.path = new PointArray();
    this.oldPoint = new Point(this.locationX,this.locationY, this.locationZ);
  }

  void draw() {
    this.fly(); // tick
    pushMatrix();
      fill(255,200,200);
      translate(this.locationtX,this.locationtY,this.locationtZ);
      translate(0,0,50);
      rotate(this.locationR);
      fill(color(abs(this.locationtX),abs(this.locationtY),20,40));
      rotateX(radians(-tick));
      rotateY(radians(-tick));
      rotateZ(radians(-tick));
      gfx.box(node);
      float r = random(255);
      drawBreadCumb();
    popMatrix();
    
  }
  
  void drawBreadCumb(){
    Point ipoint = new Point(this.locationX,this.locationY, this.locationZ);
    //TODDO
    //if (ipoint.moved(oldPoint, 100)){
    if (ipoint.moved(ipoint, 100)){
      this.path.add(ipoint);
      oldPoint = ipoint;
    }
    for (Point each : this.path){
      line(this.locationX,this.locationY,this.locationZ,this.locationX,this.locationY,this.locationZ);
    }
  }
  
  void setHost(Sniffer sniffer) {
    if (sniffer.getHostname() != this.u_hostname){
      this.setnewloc(1);
      this.u_hostname = sniffer.getHostname();
      // here is the location next to the sniffer
      float angle = 2*PI/sniffer.getNodesAttached();
      float nodeX = cos(angle)*SNIFFER_SIZE*3;
      float nodeY = sin(angle)*SNIFFER_SIZE*3; 
      
      this.locationX = sniffer.getLocationX();
      this.locationY = sniffer.getLocationY();
      this.locationZ = sniffer.getLocationZ();
    }
  }
  float targetLocation(float location, float target){
    if (location > target+5){
      return location-this.flyRate;
    }else if (location < target-5){
      return location+this.flyRate;
    }else{
      return location;
    }
  }
  void fly(){
    this.locationtX = targetLocation(this.locationtX, this.locationX);
    this.locationtY = targetLocation(this.locationtY, this.locationY);
    this.locationtZ = targetLocation(this.locationtZ, this.locationZ);
  }
  
  
  String getMac() {
    return this.u_mac;
  }
  String getHostname() {
    return this.u_hostname;
  }
  int getnewloc(){
   return this.newloc; 
  }
  void setnewloc(int change){
   this.newloc = change; 
  }
  
  int getid() {
    return this.u_id;
  }
  
  float getLocationX() {
    return this.locationX;
  }

  float getLocationY() {
    return this.locationY;
  }
  float getLocationZ() {
    return this.locationZ;
  }
  
  void setMode(String mode){
    this.m_mode = mode;
  }
  String getMode(){
    return this.m_mode;
  }
}
